﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ForumDAL
{
    public class AnswerDAL
    {
        public List<ForumDomainModels.Answer> GetAnswersByPost(ForumDomainModels.Post post)
        {
            string connectionString = Properties.Settings.Default.connectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = "SELECT [Id],[Content],[DatePublished],[PostId],[AuthorId] "+
                    "FROM [ForumDb].[dbo].[Answers] "+
                    "WHERE PostId = @postId";
                using (SqlCommand command = new SqlCommand(sql,connection))
                {
                    command.Parameters.Add(new SqlParameter("postId", post.Id));
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (!reader.HasRows)
                        {
                            throw new CustomExceptions.NoRowsRetrieved();
                        }
                        else
                        {
                            List<ForumDomainModels.Answer> answers = new List<ForumDomainModels.Answer>();
                            while (reader.Read())
                            {
                                ForumDomainModels.Answer currentAnswer = new ForumDomainModels.Answer();
                                currentAnswer.Id = (int)reader["Id"];
                                currentAnswer.Content = (string)reader["Content"];
                                currentAnswer.DatePublished = (DateTime)reader["DatePublished"];
                                currentAnswer.PostId = (int)reader["PostId"];
                                currentAnswer.AuthorId = (int)reader["AuthorId"];
                                answers.Add(currentAnswer);
                            }
                            return answers;
                        }
                    }
                }
            }
        }

        public void InsertAnswer(ForumDomainModels.Answer answer)
        {
            string connectionString = Properties.Settings.Default.connectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = "INSERT INTO Answers([Content],[DatePublished],[PostId],[AuthorId]) "+
                    "VALUES(@content,@datePublished,@postId,@authorId)";

                using (SqlCommand command = new SqlCommand(sql,connection))
                {
                    command.Parameters.Add(new SqlParameter("content",answer.Content));
                    command.Parameters.Add(new SqlParameter("datePublished",answer.DatePublished));
                    command.Parameters.Add(new SqlParameter("postId",answer.PostId));
                    command.Parameters.Add(new SqlParameter("authorId",answer.AuthorId));
                    int affectedRows = command.ExecuteNonQuery();
                    if (affectedRows == 0)
                    {
                        throw new CustomExceptions.NotInsertedRowsInDatabase();
                    }
                }
            }
        }
    }
}
